package com.yellowpage.bigdata.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.bigdata.data.NspFatallogRequsetSum;
import com.yellowpage.bigdata.service.NspFatallogRequsetSumService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;
import com.yellowpage.white.struts.BaseAction;

public class NspAbilityRequestDaySumAction extends BaseAction{
	private NspFatallogRequsetSum nspFatallogRequsetSum;
	private NspFatallogRequsetSumService nspFatallogRequsetSumService;
	private RegionService regionService;
	private String selectedId;
	private String query_logDate;
	private String query_requestType;
	private String query_logDate_end;
	private List<Map> resultList = new ArrayList<Map>();
	private Map<String,String> channelMap = new HashMap<String,String>();
	private Map<String,String> requestTypeList;
	private String query_channel;
	private Map requestTypeMap = new HashMap();
	private String flag;
	private String channelCode;
	private String regionName;
	private Map<String, String> regionMap;
	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}
    public String getRegionSql(){
    	  Map session = ActionContext.getContext().getSession();
    	  User user = (User) session.get("userObject");
    	  String function_code= "OMC_ABILITYCALL_LOG_COUNT_DAY_OVERVIEW";
    	  String regionsql= regionService.getRegionclauseByUserFunction(user.getUserId(), function_code, "REGION_CODE");
    	 return regionsql;
    }

	public void prepare() {
		super.prepare();
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspFatallogRequsetSum = nspFatallogRequsetSumService
					.getNspFatallogRequsetSum(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {
		initPageRender();
		
		Map<String,String> queryMap = new HashMap<String,String>();
		channelMap = nspFatallogRequsetSumService.getChannelMap();
		queryMap.clear();
		String str = "";
		if(!channelCode.equals("") && query_requestType.equals("")){
			String val[] = channelCode.split(";");
			for(String ss : val){
				for(String key : channelMap.keySet()){
					if(ss.equals(channelMap.get(key))){
						str += "'" + key + "',";
					}
				}
			}
			str= str.substring(0, str.length() - 1);
			queryMap.put("requestType", nspFatallogRequsetSumService.getrequestTypeString(str));
		}else if(!query_requestType.equals("")){
				requestTypeList = nspFatallogRequsetSumService
						.getrequestTypeMap();
				String[] val = query_requestType.split(";");
				for (String str1 : val) {
					for (String ss : requestTypeList.keySet()) {
						if (requestTypeList.get(ss).equals(str1)) {
							str += "'" + ss + "',";
							break;
						}
					}
				}
				str = str.substring(0,str.length() - 1);
			queryMap.put("requestType", str);
		}else{
			queryMap.put("requestType", query_requestType);
		}
		//地区
		if(!regionName.equals("")){
			regionMap = nspFatallogRequsetSumService.getRegionMap(getRegionSql());
			String[] val = regionName.split(";");
			str = "";
			for(String ss : val){
				for(String key : regionMap.keySet()){
					if(regionMap.get(key).contains(ss)){
						str += "'" + key + "',";
						break;
					}
				}
			}
			queryMap.put("regionCode", str.substring(0,str.length() - 1));
		}else{
			queryMap.put("regionCode", regionName);
		}
		queryMap.put("logDate", query_logDate);
		queryMap.put("query_logDate_end", query_logDate_end);

		resultList = nspFatallogRequsetSumService
				.queryNspAbilityRequestSumDay(queryMap, pageRender);

		setPageRenderToRequest();

		return "query";
	}
	
	public String queryChannel(){
		initPageRender();
		if(flag.equals("1")){
			requestTypeMap = nspFatallogRequsetSumService.getrequestTypeMap();
		}else if(flag.equals("0")){
			channelMap = nspFatallogRequsetSumService.getChannelMap();
		}else{
			regionMap = nspFatallogRequsetSumService.getRegionMap(getRegionSql());
		}
		setPageRenderToRequest();
		return flag.equals("1") ? "toRequest":(flag.equals("0") ? "toChannel" : "toRegion");
	}

	public NspFatallogRequsetSum getNspFatallogRequsetSum() {
		return nspFatallogRequsetSum;
	}

	public void setNspFatallogRequsetSum(NspFatallogRequsetSum nspFatallogRequsetSum) {
		this.nspFatallogRequsetSum = nspFatallogRequsetSum;
	}

	public NspFatallogRequsetSumService getNspFatallogRequsetSumService() {
		return nspFatallogRequsetSumService;
	}

	public void setNspFatallogRequsetSumService(
			NspFatallogRequsetSumService nspFatallogRequsetSumService) {
		this.nspFatallogRequsetSumService = nspFatallogRequsetSumService;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String getQuery_logDate() {
		return query_logDate;
	}

	public void setQuery_logDate(String query_logDate) {
		this.query_logDate = query_logDate;
	}

	public String getQuery_requestType() {
		return query_requestType;
	}

	public void setQuery_requestType(String query_requestType) {
		this.query_requestType = query_requestType;
	}

	public String getQuery_logDate_end() {
		return query_logDate_end;
	}

	public void setQuery_logDate_end(String query_logDate_end) {
		this.query_logDate_end = query_logDate_end;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public Map getChannelMap() {
		return channelMap;
	}

	public void setChannelMap(Map channelMap) {
		this.channelMap = channelMap;
	}

	public Map<String,String> getRequestTypeList() {
		return requestTypeList;
	}

	public void setRequestTypeList(Map<String,String> requestTypeList) {
		this.requestTypeList = requestTypeList;
	}

	public String getQuery_channel() {
		return query_channel;
	}

	public void setQuery_channel(String query_channel) {
		this.query_channel = query_channel;
	}

	public Map getRequestTypeMap() {
		return requestTypeMap;
	}

	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getChannelCode() {
		return channelCode;
	}

	public void setChannelCode(String channelCode) {
		this.channelCode = channelCode;
	}

	public String getRegionName() {
		return regionName;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	public Map<String, String> getRegionMap() {
		return regionMap;
	}

	public void setRegionMap(Map<String, String> regionMap) {
		this.regionMap = regionMap;
	}

}
